
<template>
  <div class="tab-container">
    <div style="width:10%;float:left;">
        <el-button type="primary" size="small" @click="addOne()">添加行业分类</el-button>
        <el-input
        placeholder="输入关键字进行过滤"
        v-model="filterText"
        style="margin-top: 20px;">
        </el-input>
        <el-container>
        <el-aside width="160px">
        <el-tree      
        v-loading="treeLoading"
        element-loading-text="请给我点时间！"
        style="margin-top: 20px;"
        class="filter-tree"
        :data="items"
        :props="defaultProps"      
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        :render-content="renderContent"
        ref="tree">
        </el-tree>  
        </el-aside>
        </el-container>
        <el-dialog title="添加行业分类" :visible.sync="dialogFormVisible">
        <el-form :model="typeForm" :rules="typeRules" ref="typeForm">
            <el-form-item label="行业分类名称" label-width="">
            <el-input v-model="typeForm.typeName" auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="add()" :loading="loadingBtn">确 定</el-button>
        </div>
        </el-dialog>
    </div>
    <div style="width:90%;margin-left:185px;">
        <div class="app-container calendar-list-container">
            <div class="filter-container">
                <el-input style="width: 200px;" class="filter-item" placeholder="标题" v-model="title"></el-input>

                <el-select clearable style="width: 90px" class="filter-item" placeholder="重要性">
                    
                </el-select>


                <el-button class="filter-item" type="primary" v-waves icon="search" @click="search()">搜索</el-button>
                <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="edit"><router-link to="form">添加</router-link></el-button>
                <el-button class="filter-item" type="primary" icon="delete" @click="del()">批量删除</el-button>
            </div>
            <el-container>
                <el-main>  
            <el-table
                v-loading="tableLoading"
                element-loading-text="请给我点时间！"
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                @selection-change="handleSelectionChange">                
                <el-table-column prop="id" type="selection" width="55"></el-table-column>
                <el-table-column type="index" label="序号" width="100"></el-table-column>
                <el-table-column prop="question" label="题目" show-overflow-tooltip></el-table-column>
                <el-table-column prop="classify" label="题型" width="100"></el-table-column>
                <el-table-column prop="examType.typeName" label="分类" width="100"></el-table-column>
                <el-table-column label="操作" width="120">                    
                    <template slot-scope="props">
                      <!-- <el-button @click.native="showDetail(props.row)">查看详情</el-button> -->
                      111
                    </template>
                    <!-- <el-button>查看</el-button> -->
                </el-table-column>
            </el-table>
            <div class="navRight">
               <el-aside width="40%">
                 <div class="scroll-bar">
              <el-table class="borderdel"
              :data="tableData2" 
              style="width: 100%"
              >
              <el-table-column
              type="index"
              label="已选题"
              width="100"
              align="left">
              </el-table-column>
              <el-table-column
                prop="address"
                width="280"
                >
              </el-table-column>
              <el-table-column
                align="right"
                >
                <template slot-scope="scope">
                  <i class="el-icon-delete"></i>
                </template>
              </el-table-column>
            </el-table>
            </div>
            </el-aside>
            </div>
                </el-main>  
            </el-container>
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"                
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="pageTotal"
                style="margin-top: 20px;">
            </el-pagination>
        </div>            
    </div>
  </div>
</template>
<script>
  // import { fetchTree, fetchDel, fetchAdd } from '@/api/profession'
  // import { fetchList, fetchQuestionDel } from '@/api/question'
  // import { isvalidateRequired } from '@/utils/validate'
  import { Message } from 'element-ui'

  export default {
    data() {
      const validateTypename = (rule, value, callback) => {
        if (!isvalidateRequired(value)) {
          callback(new Error('请输入行业分类名称'))
        } else {
          callback()
        }
      }
      return {
        filterText: '',
        currentPage: 1,
        pageSize: 10,
        pageTotal: 100,
        treeLoading: false,
        tableLoading: false,
        loadingBtn: false,
        dialogFormVisible: false,
        items: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        typeForm: {
          typeName: '',
          parentId: '0'
        },
        typeRules: {
          typeName: [{ required: true, trigger: 'blur', validator: validateTypename }]
        },
        nodeData: [],
        tableData: [],
        tableData2: [
          {
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1517 弄'
          }, {
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }, {
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1516 弄'
          }
        ],
        multipleSelection: [],
        treeId: '',
        title: ''
      }
    },
    created() {
      this.getList()
      this.getQuestionList()
    },
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val)
      }
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      getQuestionList() {
        this.tableLoading = true
        this.$emit('create') // for test
        fetchList(this.treeId, this.currentPage, this.pageSize, this.title).then(response => {
          this.pageTotal = response.data.total
          this.tableData = response.data.data
          this.tableLoading = false
        })
      },
      search() {
        this.currentPage = 1
        this.getQuestionList()
      },
      del() {
        if (this.multipleSelection.length > 0) {
          var ids = []
          this.multipleSelection.forEach(function(item) {
            ids.push(item.id)
          })
          fetchQuestionDel(ids).then(response => {
            Message.success('删除题目成功')
            this.getQuestionList()
          })
        } else {
          Message.warning('请选择需要删除的题目')
        }
      },
      removeRow() {},
      getList() {
        this.treeLoading = true
        this.$emit('create') // for test
        fetchTree(this.listQuery).then(response => {
          this.items = response.data.data
          this.treeLoading = false
        })
      },
      filterNode(value, data) {
        if (!value) return true
        return data.label.indexOf(value) !== -1
      },
      append(data) {
        this.dialogFormVisible = true
        this.nodeData = data
      },
      add() {
        this.$refs.typeForm.validate(valid => {
          if (valid) {
            this.loadingBtn = true
            this.typeForm.parentId = this.nodeData.value
            fetchAdd(this.typeForm).then(response => {
              this.typeForm.typeName = ''
              if (response.data.status === 200) {
                Message.success('添加行业分类成功')
                this.dialogFormVisible = false
                this.loadingBtn = false
                if (this.typeForm.parentId === '0') {
                  this.getList()
                } else {
                  const newChild = { id: response.data.data.value, label: response.data.data.typeName, children: [] }
                  if (!this.nodeData.children) {
                    this.$set(this.nodeData, 'children', [])
                  }
                  this.nodeData.children.push(newChild)
                }
              } else {
                this.dialogFormVisible = false
                this.loadingBtn = false
                Message.error('系统异常,添加行业分类失败')
              }
            }).catch(_ => {
              this.typeForm.typeName = ''
              Message.error('系统异常,添加行业分类失败')
            })
          } else {
            return false
          }
        })
      },
      addOne() {
        this.nodeData.value = '0'
        this.dialogFormVisible = true
      },
      remove(node, data) {
        this.$confirm('删除后不可恢复,确认删除？')
          .then(response => {
            fetchDel(data.value).then(response => {
              if (response.data.status === 200) {
                Message.success('删除成功')
                const parent = node.parent
                const children = parent.data.children || parent.data
                const index = children.findIndex(d => d.value === data.value)
                children.splice(index, 1)
              } else {
                Message.error('系统异常,删除失败')
              }
            })
          }).catch(_ => {})
      },
      renderContent(h, { node, data, store }) {
        return (
          <div style='display:inline-block;text-algin:right;'>
          <span style='flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px;'>
            <span>
              <span on-click={ () => this.getQuestionListByType(data.value) } id="nodeLabel" style="text-align: left;">{node.label}</span>
            </span>
            <span style="">
              <el-button style='font-size: 12px;' type='text' on-click={ () => this.append(data) }>添加</el-button>
              <el-button style='font-size: 12px;' type='text' on-click={ () => this.remove(node, data) }>删除</el-button>
          </span>
          </span>
          </div>)
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.getQuestionList()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.getQuestionList()
      },
      getQuestionListByType(val) {
        this.currentPage = 1
        this.treeId = val
        this.getQuestionList()
      }
    }
  }
</script>
<style scoped>
  .tab-container{
    margin: 30px;
  }
  .el-aside {
    color: #333;
    text-align: center;
    overflow: hidden;
  }
  .el-aside>.el-tree{
    /* margin-top: 0!important; */
    border: 1px solid #d1dbe5;
  }
  
  .el-main {
    color: #333;
    text-align: left;
    border: 1px solid #dfe6ec;
    border-bottom: 0;
    padding: 0;
  }

  /* 右侧列表 */
    .el-aside {
    color: #333;
    border-left: 1px solid #000;
    border-top: 1px solid #000;
    position: relative;
  }
  .scroll-bar {
    overflow-y: scroll;
    width: 100%;
    height: 500px;
  }
  .borderdel>>>.el-table_1_column_1 {
    text-align: center;
    color: #fff;
  }
  .borderdel>>>.el-table_1_column_2>.cell{
    padding: 0 4px;
  }
  .borderdel>>>.el-table_1_column_1>.cell>div{
    background-color: #999;
    padding: 0;
    height: 30px;
    width: 30px;
    line-height: 30px;
    border-radius: 50%;
  }
  .borderdel>>>.el-table__body-wrapper {
    overflow: auto;
  }
  .borderdel>>>tr:nth-child(odd){
    background-color: #D7D7D7;
  }
  .navRight {

  }
</style>
<style>
    .el-aside>.el-tree>.el-tree-node #nodeLabel{
      display: inline-block;
      width: 60px;
    }
    .el-aside>.el-tree .el-tree-node__children #nodeLabel{
      display: inline-block;
      width: 30px;
      margin-right: 12px!important;
  }
</style>
